#include "Scene.hpp"
#include <QDir>

Scene::Scene(const QByteArray &screenName) : hasSceneSection(false), hasActorSection(false) {
    // goto the data dir
    QDir find = QDir::current();
    find.cdUp();
    find.cd("data/Scenes");

    if(QFile::exists(find.path()+screenName) ){
        fileName = QFileInfo(find.path()+screenName);
        if(fileName.isDir()) {
            find.cd(screenName);
             if(QFile::exists(find.path()+"Scene")) {
                 fileName = QFileInfo(find.path()+"Scene");
             }
        }
    }
    else if (QFile::exists(find.path()+screenName+".scene")) {
        fileName = QFileInfo(find.path()+screenName+".scene");
    }
}

bool Scene::preload() {
    QFile f(fileName.absoluteFilePath());
    f.open(QIODevice::ReadOnly);
    QByteArray line;
    bool rScene = false, rActors=false;
    while( !(line = f.readLine()).isEmpty() ) {
        if ( line.startsWith("[Scene]") ) {
            hasSceneSection = rScene = true;
            rActors=false;
        }
        else if ( line.startsWith("[Actors]") ) {
            hasActorSection = rActors = true;
            rScene = false;
        }
    }

    return true;
}

bool Scene::load() {
    QFile f(fileName.absoluteFilePath());
    f.open(QIODevice::ReadOnly);
    QByteArray line;
    bool rScene = false, rActors=false;
    while( !(line = f.readLine()).isEmpty() ) {
        if ( line.startsWith("[Scene]") ) {
            rScene = true;
            rActors=false;
        }
        else if ( line.startsWith("[Actors]") ) {
            rActors = true;
            rScene = false;
        }

        // if we're reading a line in the scene section.
        if(rScene) {
            ByteArrayList bal = line.split('=');
            sceneInfo[bal[0]] = bal[1];
        } else if (rActors) {
            actorNames.append(line);
        }
    }
    return true; // stub
}
